package com.dragon.logical.service.contract.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.contract.IContractArticleService;
import com.dragon.repository.entity.ContractArticle;
import com.dragon.repository.entity.ContractArticleSon;
import com.dragon.repository.mapper.ContractArticleMapper;
import com.dragon.repository.mapper.ContractArticleSonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 条款 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-21
 */
@Service
public class ContractArticleServiceImpl extends ServiceImpl<ContractArticleMapper, ContractArticle> implements IContractArticleService {

    @Autowired
    ContractArticleMapper articleMapper;
    @Autowired
    ContractArticleSonMapper articleSonMapper;

    @Override
    public List<ContractArticle> articleList() {
        List<ContractArticle> articles = articleMapper.selectList(null);
        return articles;
    }

    @Override
    public void addArticle(ContractArticle article) {
        article.setCreateTime(DateUtil.date());
        articleMapper.insert(article);
    }

    @Override
    public void updateArticle(ContractArticle article) {
        article.setUpdateTime(DateUtil.date());
        articleMapper.updateById(article);
    }

    @Override
    public void deleteArticle(Integer id) {
        ContractArticle contractArticle = articleMapper.selectById(id);
        if (contractArticle.getIsDefault() == 1){
            throw new CustomException(ResultCode.DEFAULT_ARTICLE_NOT_DELETE);
        }
        List<ContractArticleSon> contractArticleSons = articleSonMapper.selectList(new QueryWrapper<ContractArticleSon>().lambda()
                .eq(ContractArticleSon::getArticleId, id));
        if (contractArticleSons.size() != 0){
            throw new CustomException(ResultCode.ARTICLE_IS_RELEVANCY_CONTRACT);
        }
        articleMapper.deleteById(id);
    }

    @Override
    public ContractArticle viewOneDetail(Integer articleId) {
        ContractArticle contractArticle = articleMapper.selectOne(new QueryWrapper<ContractArticle>().lambda().eq(ContractArticle::getId, articleId));
        return contractArticle;
}

    @Override
    public void setUpDefault(Integer id) {
        ContractArticle contractArticle = articleMapper.selectById(id);
        if(contractArticle == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        Integer isDefault = contractArticle.getIsDefault() == 0 ? 1 : 0;
        if(isDefault==1){
            ContractArticle contractArticle1 = articleMapper.selectOne(new QueryWrapper<ContractArticle>().lambda()
                    .eq(ContractArticle::getIsDefault, 1));
            if(contractArticle1 != null){
                contractArticle1.setIsDefault(0);
                contractArticle1.setUpdateTime(DateUtil.date());
                articleMapper.updateById(contractArticle1);
            }
        }
        contractArticle.setIsDefault(isDefault);
        contractArticle.setUpdateTime(DateUtil.date());
        articleMapper.updateById(contractArticle);
    }

    @Override
    public ContractArticle getDefaultArticle() {
        ContractArticle contractArticle = articleMapper.selectOne(new QueryWrapper<ContractArticle>().lambda()
                .eq(ContractArticle::getIsDefault, 1));
        if (contractArticle == null){
            contractArticle = new ContractArticle();
        }
        return contractArticle;
    }
}
